﻿using DAL.DataProvider;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace DAL
{
    public class HoaDonDAO
    {
        public void InsertHoaDon(HoaDon hoadon)
        {
            //SqlCommand cmd = new SqlCommand("INSERT INTO HoaDon (MaHoaDon,MaKhachHang,NgayMuaHang,NgayInHoaDon,TienKhachDua,MaNVThanhToan,MaQT) VALUES"+
            //    " (@MaHD,@MaKH,@NgayMua,@NgayIn,@TienKhach,@MaNVTT,@MaQT)", DataProvider.DataAccess.GetConnection());
            SqlCommand cmd = new SqlCommand("INSERT INTO HoaDon VALUES (@MaHD,@MaKH,@NgayMua,@NgayIn,@TienKhach,@MaNVTT,@MaQT,@GGKhach)", DataProvider.DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@MaHD", hoadon.maHoaDon);
            cmd.Parameters.AddWithValue("@MaKH", hoadon.maKhachHang);
            cmd.Parameters.AddWithValue("@NgayMua", hoadon.ngayMuaHang);
            cmd.Parameters.AddWithValue("@NgayIn", hoadon.ngayInHoaDon);
            cmd.Parameters.AddWithValue("@TienKhach", hoadon.tienKhachDua);
            cmd.Parameters.AddWithValue("@MaNVTT", hoadon.maNVThanhToan);
            cmd.Parameters.AddWithValue("@MaQT", hoadon.maQT);
            cmd.Parameters.AddWithValue("@GGKhach", hoadon.giamGiaKhach);
            cmd.ExecuteNonQuery();
        }

        public void UpdateHoaDon(HoaDon hoadon)
        {
            SqlCommand cmd = new SqlCommand("UPDATE HoaDon SET " +
                "MaKhachHang=@MaKH, NgayMuaHang=@NgayMua, NgayInHoaDon=@NgayIn," +
                "TienKhachDua=@TienKhach,MaNVThanhToan=@MaNVTT, MaQT=@MaQT,MaGiamGiaKhach=@GGKhach " +
                "WHERE MaHoaDon=@MaHD", DataProvider.DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@MaHD", hoadon.maHoaDon);
            cmd.Parameters.AddWithValue("@MaKH", hoadon.maKhachHang);
            cmd.Parameters.AddWithValue("@NgayMua", hoadon.ngayMuaHang);
            cmd.Parameters.AddWithValue("@NgayIn", hoadon.ngayInHoaDon);
            cmd.Parameters.AddWithValue("@TienKhach", hoadon.tienKhachDua);
            cmd.Parameters.AddWithValue("@MaNVTT", hoadon.maNVThanhToan);
            cmd.Parameters.AddWithValue("@MaQT", hoadon.maQT);
            cmd.Parameters.AddWithValue("@GGKhach", hoadon.giamGiaKhach);
            cmd.ExecuteNonQuery();
            //if (cmd.ExecuteNonQuery() > 0)
            //    MessageBox.Show("Nhap thanh conh");
            //else
            //    MessageBox.Show("Khong thanhc ong !");
        }

        public void DeleteHoaDon(HoaDon hoadon)
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM HoaDon WHERE MaHoaDon=@MaHD", DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@MaHD", hoadon.maHoaDon);
            cmd.ExecuteNonQuery();
        }

        public List<HoaDon> GetListHoaDon()
        {
            List<HoaDon> list = new List<HoaDon>();
            DataTable dt = this.SelectAllHoaDon();
            HoaDon obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = new HoaDon();
                obj.maHoaDon = dt.Rows[i]["MaHoaDon"].ToString();
                obj.maKhachHang = dt.Rows[i]["MaKhachHang"].ToString();
                obj.ngayMuaHang = Convert.ToDateTime(dt.Rows[i]["NgayMuaHang"].ToString());
                obj.ngayInHoaDon = Convert.ToDateTime(dt.Rows[i]["NgayInHoaDon"].ToString());
                obj.tienKhachDua = Convert.ToInt32(dt.Rows[i]["TienKhachDua"].ToString());
                obj.maNVThanhToan = dt.Rows[i]["MaNVThanhToan"].ToString();
                obj.maQT = dt.Rows[i]["MaQT"].ToString();
                obj.giamGiaKhach = dt.Rows[i]["GiamGiaKhach"].ToString();
                list.Add(obj);
            }
            return list;
        }

        public DataTable SelectAllHoaDon()
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM HoaDon", DataAccess.GetConnection());
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
    }
}
